package com.cyt.work.kwmanagement.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.*;
import com.cyt.work.kwmanagement.dao.KwTeacherMessageDao;
import com.cyt.work.kwmanagement.model.po.KwKcMessage;
import com.cyt.work.kwmanagement.model.po.KwKcSetting;
import com.cyt.work.kwmanagement.model.po.KwKmMessage;
import com.cyt.work.kwmanagement.model.po.KwTeacherMessage;
import com.cyt.work.kwmanagement.model.vo.*;
import com.cyt.work.kwmanagement.service.*;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.bcel.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 考务教师表详细表-考务教师表详细表Service层实现类
 *
 * @author han jun
 * @date 2019-11-26 11:25:39
 */
@Service("kwTeacherMessageService")
public class KwTeacherMessageServiceImpl extends BaseServiceImpl<KwTeacherMessage> implements KwTeacherMessageService {

    @Resource
    private KwTeacherMessageDao kwTeacherMessageDao;

    @Autowired
    private KwKmMessageService kwKmMessageService;

    @Autowired
    private KwKcSettingService kwKcSettingService;

    @Autowired
    private KwKcMessageService kwKcMessageService;

    @Autowired
    private KwTeacherSettingService kwTeacherSettingService;


    @Autowired
    private WxEducationUserService wxEducationUserService;

    @Autowired
    public void setBaseDao(KwTeacherMessageDao kwTeacherMessageDao) {
        this.baseDao = kwTeacherMessageDao;
    }

    private static final String STRING_JK_NUM_DEFAULT = "0";
    private static final String STRING_XK_NUM_DEFAULT = "0";

    /**
     * 分页查询
     *
     * @param kwTeacherMessageVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    public ReturnResult findPage(KwTeacherMessageVo kwTeacherMessageVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(KwTeacherMessage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<KwTeacherMessage> list = kwTeacherMessageDao.selectByExample(example);
        PageInfo<KwTeacherMessage> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }




    @Override
    public ReturnResult findVoPageNew(KwTeacherMessageVo kwTeacherMessageVo) {
        List<KwTeacherMessageVo> list = kwTeacherMessageDao.selectVoList(kwTeacherMessageVo);
        //sql无法获取 在这里具体获取老师的情况
        List<KwTeacherMessageVo> newList = new ArrayList<>();
        for (KwTeacherMessageVo kwTeacherMessageVo1:list){
            List<String>jkIds = Arrays.asList(kwTeacherMessageVo1.getJkId().split(","));
            List<String>xkIds = Arrays.asList(kwTeacherMessageVo1.getXkId().split(","));
            if (kwTeacherMessageVo.getType().equals("0")){//监巡考
                if (jkIds.indexOf(kwTeacherMessageVo.getTeacherId())<0&&xkIds.indexOf(kwTeacherMessageVo.getTeacherId())<0){

                }else {
                    newList.add(getTeacherByType(kwTeacherMessageVo1));
                }

            }else if (kwTeacherMessageVo.getType().equals("1")){//监考
                if (jkIds.indexOf(kwTeacherMessageVo.getTeacherId())<0){

                }else {
                    newList.add(getTeacherByType(kwTeacherMessageVo1));
                }


            }else if (kwTeacherMessageVo.getType().equals("2")){//巡考
                if (xkIds.indexOf(kwTeacherMessageVo.getTeacherId())<0){

                }else {
                    newList.add(getTeacherByType(kwTeacherMessageVo1));
                }

            }

        }

        //对时间排序
        List<String> timeList = new  ArrayList<>();
       for (KwTeacherMessageVo kwTeacherMessageVonew:newList){
           if (timeList.indexOf(kwTeacherMessageVonew.getBeginTime().substring(0,10))<0){
               timeList.add(kwTeacherMessageVonew.getBeginTime().substring(0,10));
           }

       }

       //放入map 中
        List<Map> mapList = new ArrayList<>();

        for (String string:timeList){
            Map map = new HashMap();
            List<KwTeacherMessageVo> newkwList = new ArrayList<>();
            for (KwTeacherMessageVo kwTeacherMessageVonew:newList){
                if (string.equals(kwTeacherMessageVonew.getBeginTime().substring(0,10))){
                    newkwList.add(kwTeacherMessageVonew);
                }
            }
            map.put("time",string);
            map.put("list",newkwList);
            mapList.add(map);

        }

        return  new ReturnResult(mapList);
    }
    @Override
    public ReturnResult findVoPageSign(KwTeacherMessageVo kwTeacherMessageVo) throws ParseException {
        List<KwTeacherMessageVo> list = kwTeacherMessageDao.selectVoList(kwTeacherMessageVo);
        //sql无法获取 在这里具体获取老师的情况
        List<KwTeacherMessageVo> newList = new ArrayList<>();
        for (KwTeacherMessageVo kwTeacherMessageVo1:list){
            List<String>jkIds = Arrays.asList(kwTeacherMessageVo1.getJkId().split(","));
            List<String>xkIds = Arrays.asList(kwTeacherMessageVo1.getXkId().split(","));
                if (jkIds.indexOf(kwTeacherMessageVo.getTeacherId())<0){

                }else {
                    newList.add(getTeacherByType(kwTeacherMessageVo1));
                }
        }
        List<KwTeacherMessageVo> newListArray = new ArrayList<>();
        for (KwTeacherMessageVo kwTeacherMessageVo1:newList){
            //与当前时间的比较  来获取状态
            //type 去判断选中的状态情况 3是全部 0是未开始  1是已签到 2是已结束
           int a =  checkDataNumber(kwTeacherMessageVo1.getBeginTime(),kwTeacherMessageVo1.getEndTime());
           if (a==1){
               kwTeacherMessageVo1.setKwType("1");
           }else if(a==0){
               kwTeacherMessageVo1.setKwType("0");
           }else if(a==2){
               kwTeacherMessageVo1.setKwType("2");
           }
        }

        //几种列表类型的判断
        if (kwTeacherMessageVo.getType().equals("4")){
            for (KwTeacherMessageVo kwTeacherMessageVo1:newList){
                    newListArray.add(kwTeacherMessageVo1);
            }
        }else {
            for (KwTeacherMessageVo kwTeacherMessageVo1:newList){
                if (kwTeacherMessageVo.getType().equals(kwTeacherMessageVo1.getKwType())){
                    newListArray.add(kwTeacherMessageVo1);
                }


            }
        }

        return  new ReturnResult(newListArray);

    }

    private  int checkDataNumber(String beginTime,String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String nowtime =   sdf.format( new Date() );
        String begintime = beginTime.substring(0,16);
        String endtime = endTime.substring(0,16);
        Date nowdata = sdf.parse(nowtime);
        Date begindata = sdf.parse(begintime);
        Date enddata = sdf.parse(endtime);
       if (!DateUtil.isSmallerThan(nowdata,begindata)){
           if (!DateUtil.isSmallerThan(nowdata,enddata)){
              return  2;
           }else {
              return 1;
           }

       }else {
           return 0;
       }
    }


    private  KwTeacherMessageVo getTeacherByType(KwTeacherMessageVo kwTeacherMessageVo1){
        List<KwTeacherMessageVo> newList = new ArrayList<>();
        List<String>jkIds = Arrays.asList(kwTeacherMessageVo1.getJkId().split(","));
        List<String>xkIds = Arrays.asList(kwTeacherMessageVo1.getXkId().split(","));
        String jkNames ="";
        String xkName ="";
        for (String str:jkIds){
            WxEducationUserVo  wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setId(str);
            WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
            int i = jkIds.indexOf(str);
            if(i == 0){
                jkNames = wxEducationUser.getName();
            }else{
                jkNames = jkNames+","+wxEducationUser.getName();
            }
        }

        for (String str:xkIds){
            WxEducationUserVo  wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setId(str);
            WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
            int j = xkIds.indexOf(str);
            if(j == 0){
                xkName = wxEducationUser.getName();
            }else{
                xkName = xkName+","+wxEducationUser.getName();
            }
        }
        kwTeacherMessageVo1.setJkName(jkNames);
        kwTeacherMessageVo1.setXkName(xkName);


        return  kwTeacherMessageVo1;
    }










    //这里的考务还要按照时间分配的情况
    @Override
    public ReturnResult findVoPage(KwTeacherMessageVo kwTeacherMessageVo, int pageNum, int pageSize) {
        List<KwTeacherMessageVo> list = kwTeacherMessageDao.selectVoList(kwTeacherMessageVo);
        List<KwTeacherMessageVo> lastlist = new ArrayList<>();
        //sql无法获取 在这里具体获取老师的情况
        for (KwTeacherMessageVo kwTeacherMessageVo1:list){
            List<String>jkIds = Arrays.asList(kwTeacherMessageVo1.getJkId().split(","));
            List<String>xkIds = new ArrayList<>();
            if (StringUtils.isNotBlank(kwTeacherMessageVo1.getXkId())){
                xkIds = Arrays.asList(kwTeacherMessageVo1.getXkId().split(","));
            }
            String jkNames ="";
            String xkName ="";
            for (String str:jkIds){
                WxEducationUserVo  wxEducationUserVo = new WxEducationUserVo();
                wxEducationUserVo.setId(str);
                WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
                int i = jkIds.indexOf(str);
                if(i == 0){
                    jkNames = wxEducationUser.getName();
                }else{
                    jkNames = jkNames+","+wxEducationUser.getName();
                }
            }

            for (String str:xkIds){
                WxEducationUserVo  wxEducationUserVo = new WxEducationUserVo();
                wxEducationUserVo.setId(str);
                WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
                int j = xkIds.indexOf(str);
                if(j == 0){
                    xkName = wxEducationUser.getName();
                }else{
                    xkName = xkName+","+wxEducationUser.getName();
                }
            }
            kwTeacherMessageVo1.setJkName(jkNames);
            kwTeacherMessageVo1.setXkName(xkName);
            kwTeacherMessageVo1.setBeginTime(kwTeacherMessageVo1.getBeginTime().substring(0,16));
            kwTeacherMessageVo1.setEndTime(kwTeacherMessageVo1.getEndTime().substring(0,16));
        }

        if (StringUtils.isNotBlank(kwTeacherMessageVo.getJkName())&&StringUtils.isNotBlank(kwTeacherMessageVo.getXkName())){
            for (KwTeacherMessageVo kwTeacherMessageVo1:list){
                if (kwTeacherMessageVo1.getJkName().indexOf(kwTeacherMessageVo.getJkName())!=-1&&kwTeacherMessageVo1.getXkName().indexOf(kwTeacherMessageVo.getXkName())!=-1){
                    lastlist.add(kwTeacherMessageVo1);
                }
            }
            PageInfo<KwTeacherMessageVo> page = new PageInfo<>(lastlist);
            return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());


        }else if (StringUtils.isBlank(kwTeacherMessageVo.getJkName())&&StringUtils.isNotBlank(kwTeacherMessageVo.getXkName())){
            for (KwTeacherMessageVo kwTeacherMessageVo1:list){
                if (kwTeacherMessageVo1.getXkName().indexOf(kwTeacherMessageVo.getXkName())!=-1){
                    lastlist.add(kwTeacherMessageVo1);
                }
            }
            PageInfo<KwTeacherMessageVo> page = new PageInfo<>(lastlist);
            return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
        }else if (StringUtils.isNotBlank(kwTeacherMessageVo.getJkName())&&StringUtils.isBlank(kwTeacherMessageVo.getXkName())){
            for (KwTeacherMessageVo kwTeacherMessageVo1:list){
                if (kwTeacherMessageVo1.getJkName().indexOf(kwTeacherMessageVo.getJkName())!=-1){
                    lastlist.add(kwTeacherMessageVo1);
                }
            }
            PageInfo<KwTeacherMessageVo> page = new PageInfo<>(lastlist);
            return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
        }else {
            PageInfo<KwTeacherMessageVo> page = new PageInfo<>(list);
            return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());

        }


    }


    @Override
    public List<KwTeacherStExportVo> exportTeacherList(KwTeacherMessageVo kwTeacherMessageVo){
        List<KwTeacherMessageVo> list = kwTeacherMessageDao.selectVoList(kwTeacherMessageVo);
        //sql无法获取 在这里具体获取老师的情况
        for (KwTeacherMessageVo kwTeacherMessageVo1:list){
            List<String>jkIds = Arrays.asList(kwTeacherMessageVo1.getJkId().split(","));
            List<String>xkIds = new ArrayList<>();
            if (StringUtils.isNotBlank(kwTeacherMessageVo1.getXkId())){
                xkIds = Arrays.asList(kwTeacherMessageVo1.getXkId().split(","));
            }
            String jkNames ="";
            String xkName ="";
            for (String str:jkIds){
                WxEducationUserVo  wxEducationUserVo = new WxEducationUserVo();
                wxEducationUserVo.setId(str);
                WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
                int i = jkIds.indexOf(str);
                if(i == 0){
                    jkNames = wxEducationUser.getName();
                }else{
                    jkNames = jkNames+","+wxEducationUser.getName();
                }
            }

            for (String str:xkIds){
                WxEducationUserVo  wxEducationUserVo = new WxEducationUserVo();
                wxEducationUserVo.setId(str);
                WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
                int j = xkIds.indexOf(str);
                if(j == 0){
                    xkName = wxEducationUser.getName();
                }else{
                    xkName = xkName+","+wxEducationUser.getName();
                }
            }
            kwTeacherMessageVo1.setJkName(jkNames);
            kwTeacherMessageVo1.setXkName(xkName);
            kwTeacherMessageVo1.setBeginTime(kwTeacherMessageVo1.getBeginTime().substring(0,16));
            kwTeacherMessageVo1.setEndTime(kwTeacherMessageVo1.getEndTime().substring(0,16));
        }

        List<KwTeacherStExportVo> newList = new ArrayList<>();
        for (KwTeacherMessageVo kwTeachernew:list){
            KwTeacherStExportVo kwTeacherStExportVo = new KwTeacherStExportVo();
            kwTeacherStExportVo.setKmmc(kwTeachernew.getKmmc());
            kwTeacherStExportVo.setBeginTime(kwTeachernew.getBeginTime());
            kwTeacherStExportVo.setEndTime(kwTeachernew.getEndTime());
            kwTeacherStExportVo.setKcNum(kwTeachernew.getKcNum());
            kwTeacherStExportVo.setKcStu(kwTeachernew.getKcStu());
            kwTeacherStExportVo.setKcPlace(kwTeachernew.getKcPlace());
            kwTeacherStExportVo.setJkName(kwTeachernew.getJkName());
            kwTeacherStExportVo.setXkName(kwTeachernew.getXkName());
            newList.add(kwTeacherStExportVo);

        }
        return newList;


    }



    /**
     * list查询
     *
     * @param kwTeacherMessageVo 接受参数对象
     * @return List<KwTeacherMessage>
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    public List<KwTeacherMessage> findList(KwTeacherMessageVo kwTeacherMessageVo) {
        Example example = new Example(KwTeacherMessage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(kwTeacherMessageVo.getKsId())){
            criteria.andEqualTo("ksId", kwTeacherMessageVo.getKsId());
        }
        if (StringUtils.isNotBlank(kwTeacherMessageVo.getKmId())){
            criteria.andEqualTo("kmId", kwTeacherMessageVo.getKmId());
        }
        return kwTeacherMessageDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param kwTeacherMessageVo 接受参数对象
     * @return List<KwTeacherMessage>
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    public List<KwTeacherMessageVo> findVoList(KwTeacherMessageVo kwTeacherMessageVo) {
        return kwTeacherMessageDao.selectVoList(kwTeacherMessageVo);
    }

    /**
     * 单个对象查询
     *
     * @param kwTeacherMessageVo 接受参数对象
     * @return KwTeacherMessage
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    public KwTeacherMessage findOne(KwTeacherMessageVo kwTeacherMessageVo) {
        KwTeacherMessage kwTeacherMessage = new KwTeacherMessage();
        if (StringUtils.isBlank(kwTeacherMessageVo.getId())) {
            kwTeacherMessage.setId(kwTeacherMessageVo.getId());
        }
        return kwTeacherMessageDao.selectOne(kwTeacherMessage);
    }

    /**
     * 单个对象查询
     *
     * @param kwTeacherMessageVo 接受参数对象
     * @return KwTeacherMessage
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    public KwTeacherMessageVo findVoOne(KwTeacherMessageVo kwTeacherMessageVo) {
        return kwTeacherMessageDao.selectVoOne(kwTeacherMessageVo);
    }

    /**
     * 添加或更新
     *
     * @param kwTeacherMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwTeacherMessageVo kwTeacherMessageVo, SysUser sysUser) {
        if (StringUtils.isBlank(kwTeacherMessageVo.getId())) {
            KwTeacherMessage kwTeacherMessage = new KwTeacherMessage(sysUser);
            kwTeacherMessage.setApplyId(kwTeacherMessageVo.getApplyId());
            kwTeacherMessage.setKsId(kwTeacherMessageVo.getKsId());
            kwTeacherMessage.setJkId(kwTeacherMessageVo.getJkId());
            kwTeacherMessage.setXkId(kwTeacherMessageVo.getXkId());
            kwTeacherMessage.setKcId(kwTeacherMessageVo.getKcId());
            kwTeacherMessage.setKmId(kwTeacherMessageVo.getKmId());
            kwTeacherMessageDao.insert(kwTeacherMessage);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            KwTeacherMessage kwTeacherMessage = kwTeacherMessageDao.selectByPrimaryKey(kwTeacherMessageVo.getId());
            kwTeacherMessage.setApplyId(kwTeacherMessageVo.getApplyId());
            kwTeacherMessage.setKsId(kwTeacherMessageVo.getKsId());
            kwTeacherMessage.setJkId(kwTeacherMessageVo.getJkId());
            kwTeacherMessage.setXkId(kwTeacherMessageVo.getXkId());
            kwTeacherMessage.setKcId(kwTeacherMessageVo.getKcId());
            kwTeacherMessage.setKmId(kwTeacherMessageVo.getKmId());
            kwTeacherMessage.setUpdateTime(new Date());
            kwTeacherMessage.setUpdateUser(sysUser.getId());
            kwTeacherMessageDao.updateByPrimaryKey(kwTeacherMessage);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param kwTeacherMessageVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwTeacherMessageVo kwTeacherMessageVo, SysUser sysUser) {
        //修改字段
        KwTeacherMessage kwTeacherMessage = new KwTeacherMessage();
        kwTeacherMessage.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwTeacherMessage.setUpdateUser(sysUser.getId());
        kwTeacherMessage.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwTeacherMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwTeacherMessageVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwTeacherMessageVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwTeacherMessageDao.updateByExampleSelective(kwTeacherMessage, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param kwTeacherMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwTeacherMessageVo kwTeacherMessageVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwTeacherMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwTeacherMessageVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwTeacherMessageVo.getId().split(",")));
        }
        kwTeacherMessageDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }


    /**
     * 物理删除所有的考务教师详情
     *
     * @param kwTeacherMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult deleteAll(KwTeacherMessageVo kwTeacherMessageVo, SysUser sysUser) {
        Example example = new Example(KwTeacherMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwTeacherMessageVo.getKsId())) {
            criteria.andEqualTo("ksId", kwTeacherMessageVo.getKsId());
        }
        kwTeacherMessageDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);

    }

    /**
     * 更新KwTeacherMessage
     *
     * @param sysUser            用户信息
     * @param kwTeacherMessageVo 传入参数对象
     * @return void
     * @author han jun
     * @date 2018-10-30 16:18:17
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult updateKwTeacherMessage(KwTeacherMessageVo kwTeacherMessageVo, SysUser sysUser){
        if (StringUtils.isNotBlank(kwTeacherMessageVo.getJkId())) {
            KwTeacherMessage kwTeacherMessage = findList(kwTeacherMessageVo).get(0);
            kwTeacherMessage.setJkId(kwTeacherMessageVo.getJkId());
            kwTeacherMessage.setUpdateUser(sysUser.getId());
            kwTeacherMessage.setUpdateTime(new Date());
            kwTeacherMessageDao.updateByPrimaryKey(kwTeacherMessage);
        } else if (StringUtils.isNotBlank(kwTeacherMessageVo.getXkId())) {
            List<KwTeacherMessage> kwTeacherMessageList = findList(kwTeacherMessageVo);
            for (int i = 0; i < kwTeacherMessageList.size(); i++) {
                KwTeacherMessage kwTeacherMessage = kwTeacherMessageList.get(i);
                kwTeacherMessage.setXkId(kwTeacherMessageVo.getXkId());
                kwTeacherMessage.setUpdateUser(sysUser.getId());
                kwTeacherMessage.setUpdateTime(new Date());
                kwTeacherMessageDao.updateByPrimaryKey(kwTeacherMessage);
            }
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
    }


    /**
     * 分配老师
     *
     * @param sysUser            用户信息
     * @param kwTeacherMessageVo 传入参数对象
     * @return ResultDto
     * @author han jun
     * @date 2018-10-30 10:35:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult startDistributeTeacher(KwTeacherMessageVo kwTeacherMessageVo, SysUser sysUser){
        KwKmMessageVo kwKmMessageVo = new KwKmMessageVo();
        kwKmMessageVo.setKsId(kwTeacherMessageVo.getKsId());
        List<KwKmMessage> kwKmMessageList = kwKmMessageService.listExample(kwKmMessageVo);
        if (kwKmMessageList == null || kwKmMessageList.size() == 0) {
            return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
        }
        //获取考场设置信息
        KwKcSettingVo kwKcSettingVo = new KwKcSettingVo();
        kwKcSettingVo.setKsId(kwTeacherMessageVo.getKsId());
        KwKcSetting kwKcSetting = kwKcSettingService.findOne(kwKcSettingVo);
        if (kwKcSetting == null) {
            return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
        }
        //获取考场详细信息
        KwKcMessageVo kwKcMessageVo = new KwKcMessageVo();
        kwKcMessageVo.setKsId(kwTeacherMessageVo.getKsId());
        List<KwKcMessage> kwKcMessageList = kwKcMessageService.listExample(kwKcMessageVo);
        if (kwKcMessageList == null || kwKcMessageList.size() == 0) {
            return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
        }
        //获取监考巡考老师设置信息
        KwTeacherSettingVo kwTeacherSettingVo = new KwTeacherSettingVo();
        kwTeacherSettingVo.setKsId(kwTeacherMessageVo.getKsId());
        List<KwTeacherSettingVo> kwTeacherSettingVOList = kwTeacherSettingService.findVoList(kwTeacherSettingVo);
        if (kwTeacherSettingVOList == null || kwTeacherSettingVOList.size() == 0) {
            return new ReturnResult(BaseConstant.DATABASE_ERROR_INSERT);
        }
        List<KwTeacherSettingVo> jkKwTeacherSettingVOList = new ArrayList<>();
        List<KwTeacherSettingVo> xkKwTeacherSettingVOList = new ArrayList<>();
        for (int i = 0; i < kwTeacherSettingVOList.size(); i++) {
            KwTeacherSettingVo kwTeacherSettingVO = kwTeacherSettingVOList.get(i);
            kwTeacherSettingVO.setJkNum(STRING_JK_NUM_DEFAULT);
            kwTeacherSettingVO.setXkNum(STRING_XK_NUM_DEFAULT);
            if (StringUtils.isNotBlank(kwTeacherSettingVO.getJkId())) {
                jkKwTeacherSettingVOList.add(kwTeacherSettingVO);
            }
            if (StringUtils.isNotBlank(kwTeacherSettingVO.getXkId())) {
                xkKwTeacherSettingVOList.add(kwTeacherSettingVO);
            }
        }
        //分配监考老师
        Random rand = new Random();
        int jkNum = Integer.parseInt(kwKcSetting.getJkNum());
        List<KwTeacherSettingVo> jkAfterSelect = new ArrayList<>();
        List<KwTeacherMessageVo> kwTeacherMessageDTOList = new ArrayList<>();
        for (int i = 0; i < kwKmMessageList.size(); i++) {
            KwKmMessage kwKmMessage = kwKmMessageList.get(i);
            if (jkKwTeacherSettingVOList.size() == 0) {
                jkKwTeacherSettingVOList = jkAfterSelect;
                jkAfterSelect = new ArrayList<>();
            }
            for (int j = 0; j < kwKcMessageList.size(); j++) {
                KwKcMessage kwKcMessage = kwKcMessageList.get(j);
                KwTeacherMessageVo dto = new KwTeacherMessageVo();
                dto.setKsId(kwTeacherMessageVo.getKsId());
                dto.setKcId(kwKcMessage.getId());
                dto.setKmId(kwKmMessage.getId());
                for (int k = 0; k < jkNum; k++) {
                    int index = rand.nextInt(jkKwTeacherSettingVOList.size());
                    KwTeacherSettingVo kwTeacherSettingVO = jkKwTeacherSettingVOList.get(index);
                    String jkId = dto.getJkId();
                    if (StringUtils.isNotBlank(jkId)) {
                        jkId += "," + kwTeacherSettingVO.getJkId();
                    } else {
                        jkId = kwTeacherSettingVO.getJkId();
                    }
                    dto.setJkId(jkId);
                    jkAfterSelect.add(kwTeacherSettingVO);
                    jkKwTeacherSettingVOList.remove(index);
                    if (jkKwTeacherSettingVOList.size() == 0) {
                        jkKwTeacherSettingVOList = jkAfterSelect;
                        jkAfterSelect = new ArrayList<>();
                        for (int l = 0; l < kwTeacherMessageDTOList.size(); l++) {
                            KwTeacherMessageVo dto1 = kwTeacherMessageDTOList.get(l);
                            if (dto1.getKsId().equals(dto.getKsId()) && dto1.getKmId().equals(dto.getKmId())) {
                                for (int m = 0; m < jkKwTeacherSettingVOList.size(); m++) {
                                    KwTeacherSettingVo vo = jkKwTeacherSettingVOList.get(m);
                                    if (dto1.getJkId().indexOf(vo.getJkId()) > 0) {
                                        jkAfterSelect.add(vo);
                                        jkKwTeacherSettingVOList.remove(m);
                                    }
                                }
                            }
                        }
                        String[] id = jkId.split(",");
                        for (int l = 0; l < id.length; l++) {
                            for (int m = 0; m < jkKwTeacherSettingVOList.size(); m++) {
                                KwTeacherSettingVo vo = jkKwTeacherSettingVOList.get(m);
                                if (id[l].equals(vo.getJkId())) {
                                    jkAfterSelect.add(vo);
                                    jkKwTeacherSettingVOList.remove(m);
                                    break;
                                }
                            }
                        }
                    }
                }
                kwTeacherMessageDTOList.add(dto);
            }
        }
        //分配巡考老师
        if (xkKwTeacherSettingVOList.size() > 0) {
            int xkNum = Integer.parseInt(kwKcSetting.getXkNum());
            String[] xkTeacher = new String[kwKmMessageList.size()];
            List<KwTeacherSettingVo> xkAfterSelect = new ArrayList<>();
            for (int i = 0; i < kwKmMessageList.size(); i++) {
                KwKmMessage kwKmMessage = kwKmMessageList.get(i);
                List<KwTeacherSettingVo> xkCanSelect = new ArrayList<>();
                for (int j = 0; j < xkKwTeacherSettingVOList.size(); j++) {
                    KwTeacherSettingVo kwTeacherSettingVO = xkKwTeacherSettingVOList.get(j);
                    for (int k = 0; k < kwTeacherMessageDTOList.size(); k++) {
                        KwTeacherMessageVo dto = kwTeacherMessageDTOList.get(k);
                        if (dto.getKmId().equals(kwKmMessage.getId()) && dto.getJkId().indexOf(kwTeacherSettingVO.getXkId()) >= 0) {
                            break;
                        } else {
                            if (k == kwTeacherMessageDTOList.size() - 1) {
                                xkCanSelect.add(kwTeacherSettingVO);
                            }
                        }
                    }
                }
                if (xkCanSelect.size() > 0) {
                    for (int j = 0; j < xkAfterSelect.size(); j++) {
                        for (int k = 0; k < xkCanSelect.size(); k++) {
                            if (xkCanSelect.get(k).getId().equals(xkAfterSelect.get(j).getId())) {
                                xkCanSelect.remove(k);
                                break;
                            }
                        }
                    }
                    if (xkCanSelect.size() == 0) {
                        xkCanSelect = xkAfterSelect;
                        xkAfterSelect = new ArrayList<>();
                    }
                    for (int j = 0; j < xkNum; j++) {
                        int index = rand.nextInt(xkCanSelect.size());
                        KwTeacherSettingVo kwTeacherSettingVO = xkCanSelect.get(index);
                        if (StringUtils.isNotBlank(xkTeacher[i])) {
                            xkTeacher[i] += "," + kwTeacherSettingVO.getXkId();
                        } else {
                            xkTeacher[i] = kwTeacherSettingVO.getXkId();
                        }
                        xkAfterSelect.add(kwTeacherSettingVO);
                        xkCanSelect.remove(index);
                        if (xkCanSelect.size() == 0) {
                            xkCanSelect = xkAfterSelect;
                            xkAfterSelect = new ArrayList<>();
                            if (j != xkNum - 1) {
                                for (int k = 0; k < xkCanSelect.size(); k++) {
                                    if (xkTeacher[i].indexOf(xkCanSelect.get(k).getXkId()) > 0) {
                                        xkAfterSelect.add(xkCanSelect.get(k));
                                        xkCanSelect.remove(k);
                                    }
                                }
                            }
                        }
                    }
                } else {
                    xkTeacher[i] = null;
                }
            }
            for (int i = 0; i < kwTeacherMessageDTOList.size(); i++) {
                KwTeacherMessageVo dto = kwTeacherMessageDTOList.get(i);
                dto.setXkId(xkTeacher[i / kwKcMessageList.size()]);
            }
        }
       deleteAll(kwTeacherMessageVo,sysUser);
        for (int i = 0; i < kwTeacherMessageDTOList.size(); i++) {
            kwTeacherMessageDTOList.get(i).setApplyId(kwTeacherMessageVo.getApplyId());
            addOrUpdate(kwTeacherMessageDTOList.get(i), sysUser);
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
    }



}